package com.wechat.Util;

import java.util.List;

import com.google.gson.Gson;
import com.wechat.po.WeatherData;
import com.wechat.po.WeatherResponse;

/**
 * 百度地图天气查询接口工具类
 * @author chenpeng
 * @date 2017-02-27
 */
public class WeatherUtil {
	/**
	 * 根据城市名称查询天气
	 * @param cityName
	 * @return
	 */
	public static String queryWeather(String cityName){
		String requestUrl = "http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=E4805d16520de693a3fe707cdc962045";
		//requestUrl = requestUrl.replace("{LOCATIon}", CommonUtil.urlEncodingUTF8(cityName)).replace("{ak}", "E4805d16520de693a3fe707cdc962045");
		//调用接口获取天气预报
		String respJson = CommonUtil.httpsRequest(requestUrl, "GET", null);
		
		//通过GSON将json字符串转化成java对象
		Gson gson = new Gson();
		WeatherResponse weatherResponse = gson.fromJson(respJson, WeatherResponse.class);
		
		//解析返回结果，拼装给用户的数据
		String currentCity = weatherResponse.getResults().get(0).getCurrentCity();
		List<WeatherData> lt = weatherResponse.getResults().get(0).getWeather_data();
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(currentCity).append("天气预报").append("\n\n");
		for(WeatherData data:lt){
			buffer.append(data.getDate()).append(" ").append(data.getTemperature()).append(",");
			buffer.append(data.getWeather()).append(",");
			buffer.append(data.getWind()).append("\n");
		}
		return buffer.toString();
		
	}
	
	public static void main(String[] args) {
		System.out.println(queryWeather("北京"));
	}
}
